class MainController < ApplicationController
  def index
    @ngon_ngu = I18n.locale.to_s
    @blog_ca_phe = BaiViet.where(
      :dang_kich_hoat => true,
      :ngon_ngu => @ngon_ngu,
      :id => {:$in => DanhMuc.find_by_slug('blog_ca_phe').bai_viets.map {|o| o.id}}
    ).sort(:created_at.desc).first

    @tin_qcafes = BaiViet.where(
      :dang_kich_hoat => true,
      :ngon_ngu => @ngon_ngu,
      :id => {:$in => DanhMuc.find_by_slug('tin_qcafe').bai_viets.map {|o| o.id}}
    ).sort(:created_at.desc).limit(2).all

    @ca_phes = SanPham.where(
      :dang_kich_hoat => true,
      :id => {:$in => DanhMuc.find_by_slug('ca_phe').san_phams.map{|o| o.id}}
    ).sort(:created_at.desc).limit(6).all

    @giai_khats = SanPham.where(
      :dang_kich_hoat => true,
      :id => {:$in => DanhMuc.find_by_slug('giai_khat').san_phams.map{|o| o.id}}
    ).sort(:created_at.desc).limit(6).all

    @thuc_an_nhes = SanPham.where(
      :dang_kich_hoat => true,
      :id => {:$in => DanhMuc.find_by_slug('thuc_an_nhe').san_phams.map{|o| o.id}}
    ).sort(:created_at.desc).limit(6).all

    @so_muc_hien_thi = CauHinh.lay_gia_tri_hoac_khoi_tao('trang_chu_so_luong', 6).gia_tri.to_i;

  end

  def dang_nhap
    @title = t('dang_nhap')
    if params[:commit].present?
      nguoi_dung = NguoiDung.kiem_tra_dang_nhap(params[:email].strip, params[:mat_khau].strip)
      if nguoi_dung.present?
        session[:nguoi_dung_id] = nguoi_dung.id
        if params[:ghi_nho].present?
          cookies[:nguoi_dung_id] = {:value => nguoi_dung.id, :expires => Time.now + (3600 * 24 * 30)}
        end
        redirect_to root_url, :notice => t('dang_nhap_thanh_cong')
      else
        flash.now[:loi_dang_nhap] = t('thong_tin_dang_nhap_khong_hop_le')
      end
    end
  end

  def dang_xuat
    session.delete :nguoi_dung_id
    cookies[:nguoi_dung_id] = {:value => "", :expires => Time.now - (3600 * 24 * 30)}
    redirect_to root_url
  end

  def dang_ky
    @title = t('tao_tai_khoan')
    if params[:commit].present?
      if params[:mat_khau].strip != params[:mat_khau_confirmation].strip
        flash.now[:loi] = t 'mat_khau_moi_phai_duoc_nhap_2_lan'
      else
        if verify_recaptcha
          nguoi_dung = NguoiDung.create({
            :ten_hien_thi => params[:ten_hien_thi],
            :email=> params[:email],
            :mat_khau_hash => Digest::SHA1.hexdigest(params[:mat_khau].strip)
          })
          if nguoi_dung.valid?
            flash.now[:thanh_cong] = t('dang_ky_tai_khoan_thanh_cong')
          else
            flash.now[:loi] = t('kiem_tra_lai_cac_thong_tin_dang_ki')
          end
        else
          flash.now[:loi] = t('ma_xac_nhan_khong_dung')
        end
      end

    end
  end

  def lien_he
    @title = t('lien_he_voi_chung_toi')
    if params[:commit].present?
      if verify_recaptcha
        email_nhan_gop_y  = Email.new(:dia_chi => params[:email])
        if !email_nhan_gop_y.valid?
          flash.now[:loi] = t 'email_khong_hop_le'
        else
          if params[:ten_hien_thi].length < 4
            flash.now[:loi] = t 'do_dai_ten_phai_lon_hon_3'
          else
            if params[:noi_dung].length < 4
              flash.now[:loi] = t 'noi_dung_qua_ngan'
            else
              admin = NguoiDung.where(:is_admin => true).first
              email_lien_he = CauHinh.lay_gia_tri_hoac_khoi_tao('email_nhan_gop_y', admin.email).gia_tri
              if !email_lien_he.present?
              email_lien_he = admin.email
              end
              mailer = NguoiDungMailer.lien_he_email(params[:email].strip, params[:ten_hien_thi].strip, params[:noi_dung].strip, email_lien_he).deliver
              if mailer.present?
                flash.now[:thanh_cong] = t('chung_toi_da_nhan_duoc_thong_tin_nay')
              else
                flash.now[:loi] = t('tam_thoi_khong_the_gui_den_chung_toi')
              end
            end
          end

        end
      else
        flash.now[:loi] = t('ma_xac_nhan_khong_dung')
      end
    end
  end

  def ngon_ngu
    ngon_ngu = params[:ngon_ngu]
    cookies[:ngon_ngu] = ngon_ngu
    redirect_to :back
  end
end
